package com.wnhz.work.jy.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.MyDatePicker;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class UserBirthdayActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private TextView tv_change_birthday;
    private String oldBirthday;
    private TextView view_main_right_text;
    private View btn_main_right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_birthday);

        initData();
        initView();
    }

    private void initData() {
        oldBirthday = getIntent().getStringExtra("oldBirthday");
    }

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        view_main_right_text = (TextView) findViewById(R.id.view_main_right_text);
        view_main_right_text.setTextColor(0xffe2bb0a);
        view_main_right_text.setVisibility(View.VISIBLE);
        view_main_right_text.setText("完成");
        btn_main_right = findViewById(R.id.btn_main_right);
        btn_main_right.setVisibility(View.VISIBLE);
        btn_main_right.setOnClickListener(this);
        findViewById(R.id.tv_birthday_true).setVisibility(View.GONE);
        title.setText("修改生日");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.tv_birthday_true).setOnClickListener(this);

        tv_change_birthday = (TextView) findViewById(R.id.tv_change_birthday);
        tv_change_birthday.setOnClickListener(this);
        tv_change_birthday.setText(oldBirthday);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //修改生日
            case R.id.tv_change_birthday:
                tv_change_birthday.setEnabled(false);
                MyDatePicker datePicker = new MyDatePicker(this);
                datePicker.selectDateDialog(tv_change_birthday, "1992-01-01");
                break;

            //确定
            case R.id.btn_main_right:

                uploadByxUtils();
                break;
            default:
                break;
        }
    }

    private static final String TAG = "UserBirthdayActivity";

    private void uploadByxUtils() {
        final String brithday = tv_change_birthday.getText().toString();
        Map<String, Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        map.put("birthday", brithday);
        showDialog();
        XUtil.Post(ConfigUtil.MODIFY_BORTH, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: birthday" + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");

                    if ("1".equals(res)) {
                        MyApplication.getInstance().userBean.setBirthday(brithday);

                        //关闭Activity
                        UserBirthdayActivity.this.finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }
}
